package com.lenovo.smarttraffic.ui.activity.stage4;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarPeccancyBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.adapter.MyVpAdapter;
import com.lenovo.smarttraffic.ui.fragment.stage4.DataAnalyzeFragment;
import com.lenovo.smarttraffic.util.HttpUtils;

import java.util.ArrayList;
import java.util.List;

public class DataAnalyzeActivity extends BaseActivity {

    private List<Fragment> fragments = new ArrayList<>();
    private ViewPager viewPager;
    private LinearLayout indicate;

    public static List<CarPeccancyBean.ROWSDETAILBean> beanList;

    @Override
    protected int getLayout() {
        return R.layout.activity_real_time;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }

    private void initView() {
        initToolBar(this, "数据分析");

        viewPager = findViewById(R.id.view_pager);
        indicate = findViewById(R.id.env_indicate);
    }

    private void initData() {
        HttpUtils.post("get_all_car_peccancy", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> {
                    CarPeccancyBean carPeccancyBean = new Gson().fromJson(response.toString(), CarPeccancyBean.class);
                    beanList = carPeccancyBean.getROWS_DETAIL();

                    for (int i = 0; i < 7; i++) {
                        fragments.add(new DataAnalyzeFragment().setType(i));
                    }
                    viewPager.setOffscreenPageLimit(fragments.size());
                    viewPager.setAdapter(new MyVpAdapter(getSupportFragmentManager(), fragments));

                    for (int i = 0; i < fragments.size(); i++) {
                        ImageView imageView = new ImageView(this);
                        imageView.setPadding(10, 0, 10, 0);
                        if (i == 0) {
                            imageView.setImageResource(R.drawable.indicate_selected);
                        } else {
                            imageView.setImageResource(R.drawable.indicate);
                        }
                        imageView.setTag(i);
                        indicate.addView(imageView);
                    }

                    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                        }

                        @Override
                        public void onPageSelected(int position) {
                            ((DataAnalyzeFragment) fragments.get(position)).showChart();

                            for (int i = 0; i < fragments.size(); i++) {
                                ImageView imageView = indicate.findViewWithTag(i);
                                if (i == position) {
                                    imageView.setImageResource(R.drawable.indicate_selected);
                                } else {
                                    imageView.setImageResource(R.drawable.indicate);
                                }
                            }
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {
                        }
                    });
                });
    }
}
